Psemaphore

Section: Jan. 6, 1992 (2)
Updated: MiNT Programmer's Manual
Index Return to Main Contents
 

NAME

Psemaphore - create / use / destroy a sempahore  

SYNOPSIS

LONG Psemaphore( WORD mode, LONG id, LONG timeout );
 

DESCRIPTION

Psemaphore is a call that implements uncounted semaphores. A semaphore is used for mutual exclusion: only one process at a time may "own" a given semaphore.

For example, a semaphore may be used to protect access to data structures which are in shared memory and which are used by multiple threads in a process: before using the memory a thread must gain ownership of the guarding semaphore, and when finished the thread must release the semaphore. The semaphore would be created during initialization and destroyed during shutdown.

Semaphores are identified by an id, which is an arbitrary longword. This is the semaphore's "name." The id used to create the semaphore is the "name" of that semaphore from then on. When using semaphores, you should strive to use a longword that is unique. Using four ASCII characters which spell out something is common: 0x4b4f444f ("MODM") for instance might be the id of a semaphore that controls access to a modem. (Actually, this would be a poor choice, since there can be more than one modem in a system and this semaphore ID isn't flexible enough to handle that. "MDM1" might be better.)

Semaphore id's beginning with 0x5f (the underscore character) are reserved for operating system use.

The timeout argument is only used in Mode 2. It is ignored in other modes. A timeout of zero means "return immediately." A value of -1 means "forever" - that is, never time out. Other values are a number of milliseconds to wait for the semaphore before timing out.

The mode argument is used to tell what operation the caller desires:

MODE
ACTION
0
Create and "get" a semaphore with the given ID.
1
Destroy the indicated semaphore. The caller must own the semaphore prior to making this call.
2
Request ownership of the semaphore. Blocks until the semaphore is free or until the timeout expires. See below.
3
Release the semaphore. The caller must own the semaphore prior to making this call.
 

RETURNS

CODE
MEANING
0
Success.
ERROR
A request for a semaphore which the caller already owns.
ERANGE
The semaphore does not exist.
EACCDN
Failure. The semaphore already exists (mode 0), you don't own it (modes 1 and 3), or the request timed out (mode 2).
 

NOTES

When you create a semaphore you also own it, so you must release it before anybody else can get it. If you are blocked waiting for a semaphore (mode 2 before the timeout expires) and somebody destroys the semaphore, the call will return ERANGE to you (because the requested semaphore does not exist any more!).

When a process terminates, any semaphores it owns are released.

At most one process may own a semaphore. Ownership is not inherited by children (fork()) or across exec().

Once created, semaphores are never destroyed except upon request. Thus if a program creates a semaphore and then crashes the semaphore will never go away.

Semaphores can be implemented using named pipes and file locking. Technically, then, semaphores are redundant. The facility is included as a kernel call because it was deemed necessary to have this kind of exclusion available with little persistent state or system-call overhead.


 

Index

NAME
SYNOPSIS
DESCRIPTION
RETURNS
NOTES

This document was created by man2html, using the manual pages.
Time: 16:01:23 GMT, March 03, 2023